<?php
class ProductsAction extends PublicAction {
	//商品列表页
	public function index(){
		$uid = session('uid');
		$publishuid = I('publishuid','');
		$citycode = I('citycode');
		$citycode = $citycode?$citycode:session('citycode');
		if(!$citycode){
			$citycode = $this->getPosition('114.07', '22.62');//根据地理坐标获取城市代码
			//echo $citycode;die;
			$citycode =$citycode?$citycode: C('SITE_INFO')['citycode'];	//城市代码
			//var_dump($citycode['citycode']);die;
			session('citycode',$citycode);
		}
		$cbegin = I('cbegin','');
		if($cbegin){
			$citycodes = M('city')->where(array('begin_N'=>$cbegin))->field('code')->select(); 
			//var_dump($citycodes);die;
			foreach($citycodes as $key=>$val){
				$scodes[]=$val['code'];
			}
			$data['citycode'] = array('in',$scodes);
		}else{
			$data['citycode'] = $citycode;
		}
		//var_dump($data);die;
		$keyword = I('keyword');
		$sort = I('sort',0)>0?I('sort'):0;
		
		$productsType = I('productsType',0)>0?I('productsType'):0;
		if($productsType){
			$data['products_type']=$productsType;
		}
		
		if(!$sort){
			$sort = 'up_sort asc,jiaji_sort asc,sort_order asc,products_id desc';
			$this->sort='';
		}else{
			$sort.=',up_sort asc,jiaji_sort asc,sort_order asc,products_id desc';
			$this->sort=$sort;
		}
		
		if(tep_not_null($keyword)){
			$where['products_name']  = array('like','%'.$keyword.'%');
			$where['products_description']  = array('like','%'.$keyword.'%');
			$where['_logic'] = 'or';
			$data['_complex'] = $where;
		}
		
		if($publishuid){
			$data['uid']=$publishuid;
		}
		
		
		$data['products_status']=1;
		$data['products_close'] = 0;
		$data['show_flag'] = 0;
		$cid = I('cid',0)>0?I('cid'):0;
		//echo $cid;die;
		/* echo $sort;
		var_dump($data);die; */
		if($cid){
			$prolist = D('Products')->getPidList($cid);//
			$prol = array();
			foreach($prolist as $key=>$vo){
				$prol[]=$vo['products_id'];
			}
			//var_dump($prol);die;
			$data['products_id'] = array('IN',$prol);
			$productlist = D('Products')->getProductsList($data,$sort,'0,5');
			//echo M('products')->getLastSql();die;
			/* echo "<pre>";
			var_dump($prolist);die; */
			$cate = D('CategoryView')->getOne(array('c.categories_id'=>$cid,'cd.language_id'=>L('lan_id')));
			
		}else{
			$productlist =  D('Products')->getProductsList($data,$sort,'0,5');
			//echo M('products')->getLastSql();die;
		}
		//获取产品分类
		$category = D('CategoryView')->getAllc(array('cd.language_id'=>L('lan_id'),'c.categories_status'=>1),'c.sort_order asc');
		$this->category = $category;
		//var_dump($productlist);die;
		$this->productlist = $productlist;
		$this->cate = $cate;
		$this->cid = $cid>-1?$cid:'';
		$this->products_type = $productsType;
		$this->citycode = $citycode;
		$this->cbegin = $cbegin;
		$this->display();
		
	}
	
	//商品详情页
	public function detail(){
		$pid = I('pid');
		if(!$pid){
			$this->redirect('Products/index');
		}
		$publishuid = M('Products')->where(array('products_id'=>$pid))->field('uid')->find();
		$uid = session('uid');
		if($publishuid['uid']!=$uid){
			//观看记录加1
			M('Products')->where(array('products_id'=>$pid))->setInc('viewd');
			//记录浏览记录
			if($uid){
				//echo $uid;die;
				$data['products_id']=$pid;
				$data['uid'] = $uid;
				$data['browsing_time'] = date('Y-m-d H:i:s',time());
				M('browsing_record')->add($data);
				
			}
		}
		
		
		$productsDetail = D('Products')->getProductsDetail(array('products_id'=>$pid));
		//var_dump($productsDetail);die;
		if($productsDetail['products_type']==2){//拍卖商品获取拍卖纪录
			$nowprice = M('auction')->where(array('products_id'=>$pid))->field('price,add_date')->order('auction_id desc')->limit('0,3')->select();//limit(1)->find();
			//echo M('auction')->getLastSql();die;
		}
		//获取商品的图片集
		$productImages = M('products_images')->where(array('products_id'=>$pid))->select();
		//var_dump($productImages);die;
		//var_dump($nowprice);die;
		//获取产品的分类
		$cid = M('products_to_categories')->where(array('products_id'=>$pid))->field('categories_id')->find();
		//var_dump($cid);die;
		//echo $cid['categories_id'];die;
		//获取相同分类商品
		$prolist = D('Products')->getPidList($cid['categories_id']);
		//var_dump($prolist);die;
		$prol = array();
		foreach($prolist as $key=>$vo){
			if($vo['products_id']!=$pid){
				$prol[]=$vo['products_id'];
			}
		}
		//var_dump($prol);die;
		$data1['p.products_id'] = array('IN',$prol);
		$productlist = D('Products')->getProductsList($data1,'p.up_sort asc,p.jiaji_sort asc,p.sort_order asc,p.products_id desc','0,2');
	
		$this->productsDetail = $productsDetail;
		$this->nowprice = $nowprice?$nowprice:0;//$nowprice['price']?$nowprice['price']:$productsDetail['products_paimai_price'];
		$this->productImages = $productImages;
		/*  echo "<pre>";
		var_dump($productsDetail);die;  */
		$this->productlist = $productlist;
		$this->cid = $cid;
		$this->uid = $uid;
		$this->msg = I('msg',0);
		$this->display();
		
	}
	
	//商品拍卖页
	public function auction(){
		$uid = session('uid');
		$pid = I('pid');
		$this->checkuid($uid,U('Products/auction',array('pid'=>$pid)));
		
		if(!$pid){
			$this->redirect('Products/index');
		}
		if($_POST){//提交拍卖结果
			$publishid = M('products')->where(array('products_id'=>$pid,'uid'=>$uid))->find();
			if($publishid){//如果是发布者自己的商品，不能出价
				$this->msg = '该商品是您自己发布的商品，不能够自己给出价';
				$nowprice = M('auction a')->join('products p ON a.products_id=p.products_id')->where(array('a.products_id'=>$pid))->field('a.price,p.products_price,p.products_paimai_price')->order('auction_id desc')->limit('1')->find();
				$this->nowprice = $nowprice;
				$this->pid = $pid;
				$this->display();
			}else{
				$data['products_id']=$pid;
				$data['price']=I('price');
				$data['uid'] = $uid;
				$data['add_date'] = date('Y-m-d H:i:s',time());
				$result = M('auction')->add($data);
				if($result){
					$this->redirect('Products/detail',array('pid'=>$pid,'msg'=>'恭喜您！，出价成功'));//$this->msg = '恭喜您！，出价成功';
				
				}else{
					$this->msg = '出价失败，联系管理员';
				}
				$nowprice = M('auction a')->join('products p ON a.products_id=p.products_id')->where(array('a.products_id'=>$pid))->field('a.price,p.products_price,p.products_paimai_price')->order('a.auction_id desc')->limit('1')->find();
				$this->nowprice = $nowprice;
				$this->pid = $pid;
				$this->display();
			}
			
		}else{
			$paimai = M('auction')->where(array('uid'=>$uid,'products_id'=>$pid))->order('auction_id desc')->limit('1')->find();
			if($paimai){
				$msg='您现在的价格是最高价';
			}
			$nowprice = M('auction')->table('auction a')->join('products p ON a.products_id=p.products_id')->where(array('a.products_id'=>$pid))->field('a.price,p.products_price,p.products_paimai_price')->order('a.auction_id desc')->limit('1')->find();
			//echo M('auction')->getLastSql();die;
			$nflag = 1;
			//var_dump($nowprice);die;
			if(!$nowprice){
				$nowprice = M('products')->where(array('products_id'=>$pid))->field('products_price,products_paimai_price')->find();
				$nflag = 0;
			}
			$this->msg = $msg?$msg:0;
			$this->nflag = $nflag;
			$this->nowprice = $nowprice;
			$this->pid = $pid;
			$this->display();
		}
	}
	
	//商品分类页
	public function cateindex(){
		//获取产品分类
		$category = D('CategoryView')->getAllc(array('cd.language_id'=>L('lan_id'),'c.categories_status'=>1),'c.sort_order asc');
		$this->category = $category;
		//$this->uid = session('uid');;
		$this->display();
	}
	
	//收藏处理
	public function collect(){
		$pid = I('pid');
		$uid = I('uid');
		$this->checkuid($uid,U('Products/detail',array('pid'=>$pid)));
		$collect = M('collect')->where(array('products_id'=>$pid,'uid'=>$uid))->find();
		if($collect['products_id']){
			$data['msg']='已经收藏过，请查看收藏记录';
		}else{
			$data1['products_id']=$pid;
			$data1['uid']=$uid;
			$data1['collect_time']=date('Y-m-d H:i:s',time());
			$result = M('collect')->add($data1);
			if($result){
				$data['msg']='恭喜您！收藏成功呢！';
			}else{
				$data['msg']='系统错误，请联系管理人员。';
			}
		}
		echo json_encode($data);
	}
	
	//留言给卖家
	public function leaveword(){
		$uid = session('uid');
		$this->checkuid($uid,U('Products/leaveword'));
		//$pid = 
		if($_POST){
			 $data['uid']=$uid;
			 $data['to_uid']=I('post.publish_uid');
			 $data['products_id']=$pid=I('post.products_id');
			 $data['comment']=I('post.comment');
			 $data['leaveword_date'] = date('Y-m-d H:i:s',time());
			 $result = M('leaveword')->add($data);
			 if($result){
			 	/* $this->msg = "留言成功！";
			 	$this->display(); */
			 	$this->redirect('Products/detail',array('pid'=>$pid,'msg'=>"留言成功！"));
			 }
		}else{
			$publishuid = I('get.publishuid','');
			
			if(!$publishuid||$publishuid==$uid){
				$this->redirect('Products/index');
			}
			$this->pid = I('get.pid');
			$this->publishuid = $publishuid;
			$this->msg = 0;
			$this->display();
		}
	
	}
	
	//商品举报
	public function report(){
		$uid = session('uid');
		$pid = I('pid');
		$this->checkuid($uid,U('Products/leaveword',array('pid'=>$pid)));
		
		if(!$pid){
			$this->redirect('Products/index');
		}else{
			if($_POST){
				$data['products_id']=$pid;
				$data['uid']=$uid;
				$data['comment']=I('post.comment');
				$data['report_date']=date('Y-m-d H:i:s',time());
				$result = M('report')->add($data);
				if($result){
					//$this->msg = '举报成功，我们会尽快处理您的举报';
					$this->redirect('Products/detail',array('pid'=>$pid,'msg'=>'举报成功，我们会尽快处理您的举报'));				
				}else{
					$this->msg='举报失败，联系管理人员';					
				}
				$this->pid = $pid;
				$this->display();
			}else{
				$this->pid = $pid;
				$this->msg = 0;
				$this->display();
			}
		}
	}
	
	//搜索商品页面
	public function search(){
		//获取商品分类
		$category = D('CategoryView')->getAllc(array('cd.language_id'=>L('lan_id'),'c.categories_status'=>1),'c.sort_order asc');
		$this->category = $category;
		$this->display();
	}

	//根据城市搜索界面
	public function scity(){
		$citycode = session('citycode');
		$nearby = M('city')->table('city c1')->join('city c2 ON c1.provincecode=c2.provincecode')->where(array('c1.code'=>$citycode))->field('c2.id,c2.code,c2.name')->select();
		//var_dump($nearby);die;
		$hotcity = M('city')->table('city c')->join('province p ON c.provincecode=p.code')->where(array('hot_flag'))->field('c.code,c.name as c_name,p.name as p_name')->select();
		//var_dump($hotcity);die;
		$this->nearby = $nearby;
		$this->hotcity = $hotcity;
		$this->citycode = $citycode;
		$this->display();
		
		
	}
}